#include "leetcode.h"

class Solution
{
public:
    int solution(vector<int> nums) {
        // your code here
        // 左右两指针向中间靠拢
        // 移动数值较小的一方
        // 因为 体积 = 较小高度 * 长度
        int l = nums.size();
        if (l < 2) return 0;
        int i = 0;
        int j = l - 1;
        int max_area = 0;
        while (i < j) {
            if (nums[i] <= nums[j]) {
                int area = nums[i] * (j - i);
                max_area = max_area < area ? area : max_area;
                ++i;
            } else {
                int area = nums[j] * (j - i);
                max_area = max_area < area ? area : max_area;
                --j;
            }
        }
        return max_area;
    }
    void test() {
        // test code
        vector<vector<int>> ts{
            {1,8,6,2,5,4,8,3,7},
            {},
            {1,},
            {4,3},
        };
        vector<int> want{49,0,0,3};
        for (int i = 0; i < ts.size(); ++i) {
            int got = solution(ts[i]);
            char _msg[100];
            sprintf(_msg, "(i: %d) got: %d want: %d", i, got, want[i]);
#ifndef NDEBU
            printf("%s\n", _msg);
#endif
            assert_msg(got == want[i], _msg);
        }
        printf("Test Done\n");
    }
};

int main(int argc, char *argv[]) {
#ifndef NDEBUG
    printf("[MAIN] argc: %d\n", argc);
    printf("[MAIN] argv: ");
    for (int i = 0; i < argc; ++i) {
        printf(argv[i]);
        if (i == argc - 1) {
            printf("\n");
        } else {
            printf(" ");
        }
    }
#endif
    Solution().test();
}

